<?php
class Kdtpush extends Base_Controller
{
    private $_appid;
    private $_appsecret;

    public function __construct()
    {
        parent::__construct();
        $this->config->load('kdt');
        $this->_appid = $this->config->item('appid');
        $this->_appsecret = $this->config->item('appsecret');
        $this->load->model('kdt_push_model');
    }

    public function getPush()
    {
        $json = file_get_contents('php://input'); //接收推送数据
        $data = json_decode($json, true);
        //立即返回
        if ($data){
            $result = array("code"=>0,"msg"=>"success") ;
            var_dump($result);
        }
        //验证消息
        $msg  = $data['msg'];
        $sign = $data['sign'];
        $this->checkInfo($msg,$sign);
        if ($data['test']===false){
            //msg是经过unicode（UTF-8）编码的消息对象,所以要进行解码
            $msg = json_decode(urldecode($msg),true);
            $save = array('id'=>$data['id'],'type'=>$data['type'],'status'=>$data['status'],'version'=>$data['version'],'msg'=>$msg);
            $save = igbinary_serialize($save);
            $this->load->model('redis_model');
            $this->redis_model->rpush('kdtpush',$save);
        }
    }

    /**
     * 判断消息是否合法
     */

    public function checkInfo($msg,$sign)
    {
        $signString = $this->_appid."".$msg."".$this->_appsecret;
        $signE = md5($signString);
        if($signE != $sign){
//            log_message('error','kdt_push: illegal info');
            exit();
        }
    }

    public function updatedb()
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(0);
        $this->kdt_push_model->procPushData();
    }

    //手动同步订单
    public function systrades()
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(0);
        ini_set('default_socket_timeout',-1);
        $this->kdt_push_model->systrades();
    }

}